<?php
/**
 * Created by PhpStorm.
 * User: huynguyen
 * Date: 3/12/14
 * Time: 9:35 PM
 */


Yii::import('zii.widgets.CMenu');

class QMenu extends CMenu
{

    public $multi = false;
    public $childOptions = array();
    public $expandClass = '';
    public $categories = array();
    /**
     * Initialize the widget
     */
    public function init()
    {
        parent::init();
    }

    public function run()
    {
        if (!$this->multi)
            $this->renderCustomNormalMenu();
        else
            $this->renderMenuRecursiveMulti();
        //parent::run();
    }

    /*
     * not top menu generate menu style
     */
    private function renderMenuRecursiveMulti()
    {
        $criteria= new CDbCriteria;
        $criteria->select = '   MENU_CODE,
                                FN_QTC_MULTI_NAME(MENU_NAME, MENU_ENG_NAME, :LANG_CODE) AS MENU_NAME,
                                FN_QTC_MULTI_NAME(MENU_SLUG, MENU_SLUG_ENG, :LANG_CODE) AS MENU_SLUG,
                                PARENT_MENU_CODE,
                                USE_YN ';
        $criteria->condition = " USE_YN = 'Y' ";
        $criteria->order = " SORT_ORDER ";
        $criteria->params =  array(':LANG_CODE'=> Yii::app()->language);
        //select level 1 menu
        $results = Menu::model()->findAll($criteria);


        $this->renderCustomMenuMulti($results);

    }

    // render menu multi
    private function renderCustomMenuMulti($results)
    {

        $data = $this->buildTreeArray($results);
        echo CHtml::openTag('ul', $this->htmlOptions) ;
        $this->buildTreeHtml($data);
        echo CHtml::closeTag('ul'). "\n" ;


    }

    // render menu multi
    private function renderCustomNormalMenu()
    {
        $criteria= new CDbCriteria;
        $criteria->select = '   MENU_CODE,
                                FN_QTC_MULTI_NAME(MENU_NAME, MENU_ENG_NAME, :LANG_CODE) AS MENU_NAME,
                                FN_QTC_MULTI_NAME(MENU_SLUG, MENU_SLUG_ENG, :LANG_CODE) AS MENU_SLUG,
                                PARENT_MENU_CODE,
                                USE_YN ';
        $criteria->condition = " USE_YN = 'Y'  AND PARENT_MENU_CODE IS NULL";
        $criteria->order = " SORT_ORDER ";
        $criteria->params =  array(':LANG_CODE'=> Yii::app()->language);
        //select level 1 menu
        $results = Menu::model()->findAll($criteria);

        // open tag li
        echo CHtml::openTag('div', $this->htmlOptions);
        // get second menu
        foreach ($results as $result) {
            $criteria->select = '   MENU_CODE,
                                    FN_QTC_MULTI_NAME(MENU_NAME, MENU_ENG_NAME, :LANG_CODE) AS MENU_NAME,
                                    FN_QTC_MULTI_NAME(MENU_SLUG, MENU_SLUG_ENG, :LANG_CODE) AS SLUG,
                                    PARENT_MENU_CODE,
                                    USE_YN ';
            $criteria->condition = " USE_YN = 'Y'  AND PARENT_MENU_CODE = :MENU_CODE ";
            $criteria->order = " SORT_ORDER ";
            $criteria->params =  array(':LANG_CODE'=> Yii::app()->language, ':MENU_CODE' => $result['MENU_CODE'] );
            //select level 2 menu
            $childItems = Menu::model()->findAll($criteria);

            //start render menu
            echo CHtml::openTag('div',  $this->childOptions);
            echo CHtml::link($result['MENU_NAME'], array('category/index','catSlug'=>$result['MENU_SLUG']));
            //begin render level 2
            if (!empty($childItems)) {
                echo CHtml::openTag('div');
                echo CHtml::openTag('ul');
                foreach ($childItems as $childItem) {
                    echo CHtml::openTag('li');
                    echo CHtml::link($childItem['MENU_NAME'], array('category/index','catSlug'=>$childItem['MENU_SLUG']));
                    echo CHtml::closeTag('li') . "\n";
                }
                echo CHtml::closeTag('ul') . "\n";

                echo CHtml::closeTag('div') . "\n";
            }
            //end render level 2
            echo CHtml::closeTag('div') . "\n";
            //end render menu
        }
        echo CHtml::closeTag('div') . "\n";
    }

    // get tree array
    private function buildTreeArray(array $elements, $parentId = null)
    {
        $branch = array();

        foreach ($elements as $element) {
            $list = array();
            if ($element['PARENT_MENU_CODE'] == $parentId) {
                $list['MENU_CODE'] = $element['MENU_CODE'];
                $list['MENU_NAME'] = $element['MENU_NAME'];
                $list['MENU_SLUG'] = $element['MENU_SLUG'];
                $list['PARENT_MENU_CODE'] = $element['PARENT_MENU_CODE'];
                $list['USE_YN'] = $element['USE_YN'];
                $children = self::buildTreeArray($elements, $element['MENU_CODE']);
                if ($children) {
                    $list['CHILDREN'] = $children;
                }
                $branch[] = $list;
            }
        }

        return $branch;
    }

    // get tree html
    private function buildTreeHtml(array $elements)
    {
        //get active category list
        $active = $this->categories;
        foreach ($elements as $element) {
            if (isset($element['CHILDREN']) && sizeof($element['CHILDREN']) > 0) {
                if(in_array($element['MENU_CODE'],$active)) $this->childOptions['class'] =(isset($this->childOptions['class'])) ? $this->childOptions['class'].' '.$this->expandClass : $this->expandClass  ;
                else $this->childOptions['class'] = (isset($this->childOptions['class'])) ? str_replace($this->expandClass, "", $this->childOptions['class']) : '';

                echo CHtml::openTag('li',  $this->childOptions);
                echo CHtml::Link($element['MENU_NAME'], array("category/index", "catSlug"=>$element['MENU_SLUG']), array("class"=>"ajaxCategory"));
                echo CHtml::openTag("span", array("class"=>"down"));
                echo CHtml::closeTag("span"). "\n";

                echo CHtml::openTag('ul');
                    self::buildTreeHtml($element['CHILDREN']);
                echo CHtml::closeTag('ul'). "\n";
                echo CHtml::closeTag('li'). "\n";

            } else {
                if(in_array($element['MENU_CODE'],$active)) $this->childOptions['class'] = (isset($this->childOptions['class'])) ? $this->childOptions['class'].' '.$this->expandClass : $this->expandClass  ;
                else $this->childOptions['class'] = (isset($this->childOptions['class'])) ? str_replace($this->expandClass, "", $this->childOptions['class']) : '';
                echo CHtml::openTag('li',  $this->childOptions);
                echo CHtml::Link($element['MENU_NAME'], array("category/index", "catSlug"=>$element['MENU_SLUG']), array("class"=>"ajaxCategory"));
                echo CHtml::closeTag('li'). "\n";
            }
        }

    }

} 